#include <bits/stdc++.h>

#define pb push_back
#define eb emplace_back
#define ep emplace
#define rep(i, x, y)  for(int i = (x); i <= (y); i++)
#define fi first
#define se second
#define in read()

using namespace std;

using ll = long long;
using db = double;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-', ch = getchar();
	while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
	return f ? -x : x;
}

const int N = 1e5 + 10;

int n, m;
int id[N], u[N], v[N], len[N], ans = 1e9, fr[N], RT, siz[N], son[N], dep[N], tim, vis[N];
bool tvis[N];
veg G[N];

void getsz(int x, int p) { vis[x] = tim; siz[x] = 1; for(auto v : G[x]) if(v.fi ^ p && !tvis[v.fi]) getsz(v.fi, x), siz[x] += siz[v.fi]; }
void getrt(int x, int p, int tsiz) { son[x] = 0; for(auto v : G[x]) if(v.fi ^ p && !tvis[v.fi]) getrt(v.fi, x, tsiz), son[x] = max(son[x], siz[v.fi]); son[x] = max(son[x], tsiz - siz[x]); if(RT == 0 || son[x] < son[RT]) RT = x; }
void dfs(int x, int tfr, int p) {
	fr[x] = tfr; for(auto v : G[x]) if(v.fi ^ p) dep[v.fi] = dep[x] + v.se, dfs(v.fi, tfr, x); 
}

void divide(int x) {
	++tim; RT = 0; getsz(x, 0); getrt(x, 0, siz[x]); x = RT; tvis[x] = true; 
	dep[x] = 0; for(auto v : G[x]) dep[v.fi] = v.se, dfs(v.fi, v.fi, x);
	int res = 0; rep(i, 1, m) len[i] = dep[u[i]] + dep[v[i]], id[i] = i, res = max(res, len[i]);
	ans = min(ans, res); sort(id + 1, id + m + 1, [](auto a, auto b) { return len[a] > len[b]; });
	int lst = 0;
	rep(i, 1, m) {
		int x = id[i];
		if(len[x] != res) break;
		if(fr[u[x]] ^ fr[v[x]]) printf("%d\n", ans), exit(0);
		else if(!lst || lst == fr[u[x]]) lst = fr[u[x]];
		else printf("%d\n", ans), exit(0);
	}
	if(lst && vis[lst] == tim) divide(lst);
}

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in", "r", stdin);
#endif
	n = in, m = in; rep(i, 2, n) { int u = in, v = in, w = in; G[u].eb(v, w); G[v].eb(u, w); }
	rep(i, 1, m) u[i] = in, v[i] = in;
	divide(1); printf("%d\n", ans);
	return 0;
}
